Java Technologies উদাহরণ সহ Hystrix Integration গাইড ও নোট

284

Hystrix হলো Netflix এর একটি লাইব্রেরি যা সার্কিট ব্রেকার প্যাটার্নের মাধ্যমে সার্ভিস ফেইল হওয়া থেকে সিস্টেমকে রক্ষা করে এবং সিস্টেমের স্থিতিশীলতা নিশ্চিত করে। এটি মাইক্রোসার্ভিস আর্কিটেকচারে বিশেষভাবে কার্যকর, যেখানে এক সার্ভিসের ব্যর্থতা পুরো সিস্টেমের উপর প্রভাব ফেলতে পারে। Hystrix সার্ভিস কলগুলির মধ্যে একটি ফেইল সিচুয়েশন মোকাবেলা করার জন্য "Fallback" পদ্ধতি ব্যবহার করে।

Hystrix Integration: একটি সাধারণ উদাহরণ

এখানে আমরা একটি উদাহরণ দেখব, যেখানে Hystrix সার্কিট ব্রেকারের মাধ্যমে একটি সার্ভিস কলের জন্য ফেইল-সেফ মেকানিজম সেটআপ করা হয়েছে।

Step 1: প্রয়োজনীয় ডিপেনডেন্সি যোগ করা

Hystrix এবং Spring Cloud Starter ব্যবহার করতে, আপনাকে আপনার pom.xml ফাইলে প্রয়োজনীয় ডিপেনডেন্সি যোগ করতে হবে।

<dependencies>
    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring Cloud Starter Hystrix -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>

    <!-- Spring Cloud Starter Eureka Client (Optional, if you're using Eureka for service discovery) -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

Step 2: Hystrix সার্কিট ব্রেকার সেটআপ

@EnableCircuitBreaker অ্যানোটেশন ব্যবহার করে সার্কিট ব্রেকার সক্রিয় করা হয়। এছাড়াও, @HystrixCommand অ্যানোটেশন ব্যবহার করে নির্দিষ্ট মেথডের জন্য সার্কিট ব্রেকার কার্যকর করা হয়। এই অ্যানোটেশনটি একটি ফেইলড সার্ভিস কলের জন্য ফ্যালব্যাক মেথড কনফিগার করতে সহায়ক।

Hystrix সার্ভিসের উদাহরণ

  1. Service Class with Hystrix Command

    প্রথমে একটি সার্ভিস তৈরি করুন, যেখানে একটি সাধারণ সার্ভিস কল থাকবে যা সফল অথবা ব্যর্থ হতে পারে। এরপর সেই মেথডে HystrixCommand ব্যবহার করা হবে।

    import com.netflix.hystrix.HystrixCommand;
    import com.netflix.hystrix.HystrixCommandGroupKey;
    import org.springframework.stereotype.Service;
    
    @Service
    public class MyService {
    
        // Default method that will fail and activate the circuit breaker
        @HystrixCommand(fallbackMethod = "fallbackMethod")
        public String callRemoteService() {
            // Simulating a remote service failure
            if (Math.random() > 0.5) {
                throw new RuntimeException("Service failure");
            }
            return "Service success";
        }
    
        // Fallback method that gets called if the service fails
        public String fallbackMethod() {
            return "Fallback response: Service is unavailable";
        }
    }
    
  2. Enable Circuit Breaker in Spring Boot Application

    সার্ভিসে সার্কিট ব্রেকার সক্রিয় করতে @EnableCircuitBreaker অ্যানোটেশন ব্যবহার করুন।

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
    
    @SpringBootApplication
    @EnableCircuitBreaker
    public class HystrixApplication {
        public static void main(String[] args) {
            SpringApplication.run(HystrixApplication.class, args);
        }
    }
    
  3. Controller to Call the Service

    একটি REST Controller তৈরি করুন, যা MyService থেকে সার্ভিস কল করবে। যদি সার্ভিস ফেইল করে, তাহলে এটি fallback মেথডের মাধ্যমে ডেটা ফিরিয়ে দেবে।

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
    
        @Autowired
        private MyService myService;
    
        @GetMapping("/call-service")
        public String callService() {
            return myService.callRemoteService();
        }
    }
    

Step 3: Configuration (Optional)

যদি আপনি সার্ভিস রেজিস্ট্রি ও ডিসকভারি ব্যবহার করতে চান, যেমন Eureka, তাহলে application.properties ফাইলে নিম্নলিখিত কনফিগারেশন যোগ করুন:

# Eureka server URL (Optional, for service discovery)
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

# Enable Hystrix dashboard for monitoring (Optional)
hystrix.stream.enabled=true

Step 4: Running the Application

  1. Run the Hystrix application: প্রথমে Spring Boot অ্যাপ্লিকেশনটি চালু করুন। আপনার সার্ভিস চালু হবে এবং আপনি /call-service URL-এ HTTP অনুরোধ পাঠালে এটি callRemoteService() মেথডে যাবে।
  2. Testing Circuit Breaker: প্রথমে সার্ভিসটি সফলভাবে কাজ করবে। পরে যখন এটি ব্যর্থ হবে, তখন সার্কিট ব্রেকার সক্রিয় হবে এবং ফ্যালব্যাক মেথডের মাধ্যমে "Fallback response: Service is unavailable" মেসেজটি দেখাবে।

Hystrix Dashboard

Hystrix-dashboard ব্যবহার করে সার্ভিসের স্বাস্থ্য পরীক্ষা এবং সার্কিট ব্রেকারের স্ট্যাটাস মনিটর করা যেতে পারে। এর মাধ্যমে আপনি সার্ভিস কলের স্ট্যাটাস এবং সার্কিট ব্রেকারের কার্যকারিতা দেখতে পারেন।

  1. Dependency Add for Hystrix Dashboard:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
    
  2. Enable Hystrix Dashboard:

    @EnableHystrixDashboard অ্যানোটেশন ব্যবহার করুন:

    import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @EnableHystrixDashboard
    public class HystrixDashboardApplication {
        public static void main(String[] args) {
            SpringApplication.run(HystrixDashboardApplication.class, args);
        }
    }
    
  3. Access Hystrix Dashboard: ড্যাশবোর্ডটি দেখতে, আপনার ব্রাউজারে http://localhost:8080/hystrix URL-এ যান।

    এখানে আপনি সার্ভিসের সার্কিট ব্রেকার স্ট্যাটাস দেখতে পারবেন, এবং http://localhost:8080/actuator/hystrix.stream থেকে স্ট্রিম দেখতে পারবেন।


Step 5: Fallback with Hystrix

আপনি যখন সার্ভিসে অতিরিক্ত ফিচার যোগ করতে চান, যেমন retry logic বা custom fallback ব্যবস্থা, আপনি Hystrix-এর ফিচার ব্যবহার করতে পারেন:

@HystrixCommand(fallbackMethod = "defaultMethod", commandProperties = {
    @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),
    @HystrixProperty(name = "execution.timeout.enabled", value = "false")})
public String riskyMethod() {
    // Your logic here that may fail
    return "Success";
}

public String defaultMethod() {
    return "Fallback response for risky method";
}

Conclusion

Hystrix সার্কিট ব্রেকার প্যাটার্ন ব্যবহার করে মাইক্রোসার্ভিস আর্কিটেকচারে ব্যর্থতার প্রতিরোধে একটি গুরুত্বপূর্ণ ভূমিকা পালন করে। এটি সার্ভিস কলগুলোর মধ্যে ব্যর্থতা ঘটে গেলে ক্লায়েন্টের কাছে একটি fallback মেকানিজম সরবরাহ করে, যাতে পুরো সিস্টেমের স্থিতিশীলতা বজায় থাকে। Hystrix ব্যবহারের মাধ্যমে, আপনি সার্ভিস কলের মধ্যে সমস্যা হলেও আপনার অ্যাপ্লিকেশনটির কার্যক্ষমতা বজায় রাখতে পারেন।

Content added By
Promotion

Are you sure to start over?

Loading...